package com.sskj.boom.ui.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.flyco.tablayout.SlidingTabLayout;
import com.hjq.toast.ToastUtils;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.sskj.boom.R;
import com.sskj.boom.R2;
import com.sskj.boom.bean.CommonBean;
import com.sskj.boom.bean.OpenAwardBean;
import com.sskj.boom.bean.StockBloomBean;
import com.sskj.boom.boomview.bean.EscapeBean;
import com.sskj.boom.boomview.widget.BoomView;
import com.sskj.boom.component.DaggerUserDataComponent;
import com.sskj.boom.falling.FallObject;
import com.sskj.boom.falling.FallingView;
import com.sskj.boom.presenter.BoomPresenter;
import com.sskj.common.adapter.MyFragmentPagerAdapter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerHorizenLineItemDecoration;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.ArithUtil;
import com.sskj.lib.util.CommonUtil;
import com.sskj.lib.widget.BottomDialog;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * @author Hey
 * Create at  2019/06/24
 */
@Route(path = RConfig.BOOM_ACTIVITY_BOOM)
public class BoomActivity extends BaseActivity<BoomPresenter> {
    @BindView(R2.id.tvRight)
    TextView tvRight;
    @BindView(R2.id.slidingTabLayout)
    SlidingTabLayout slidingTabLayout;
    @BindView(R2.id.viewPager)
    ViewPager viewPager;
    @BindView(R2.id.rl_choose_coin)
    RelativeLayout rl_choose_coin;
    @BindView(R2.id.recyclerViewHistory)
    RecyclerView recyclerViewHistory;
    @BindView(R2.id.boomView)
    BoomView boomView;
    @BindView(R2.id.iv_coin)
    ImageView ivCoin;
    @BindView(R2.id.tv_coin_name)
    TextView tvCoinName;
    @BindView(R2.id.tv_coin_usable)
    TextView tvCoinUsable;
    @BindView(R2.id.tv_money_usdt)
    TextView tvMoneyUsdt;
    @BindView(R2.id.tv_money_cny)
    TextView tvMoneyCny;
    @BindView(R2.id.tv_now_value)
    TextView tv_now_value;
    @BindView(R2.id.tv_award_poll)
    TextView tvAwardPoll;
    @BindView(R2.id.tv_show_run)
    TextView tv_show_run;
    @BindView(R2.id.tv_max)
    TextView tvMax;
    @BindView(R2.id.tv_min)
    TextView tvMin;
    @BindView(R2.id.tv_divide)
    TextView tvDivide;
    @BindView(R2.id.tv_multiply)
    TextView tvMultiply;
    @BindView(R2.id.btn_bet)
    Button btnBet;
    @BindView(R2.id.et_bet)
    EditText etBet;
    @BindView(R2.id.et_run_away)
    EditText etRunAway;
    @BindView(R2.id.tv_earn)
    TextView tvEarn;
    @BindView(R2.id.tv_chance)
    TextView tvChance;
    @BindView(R2.id.iv_boom_handred)
    ImageView iv_boom_handred;
    @BindView(R2.id.fallingView)
    FallingView fallingView;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    private SlimAdapter slimAdapter;
    //当前选择币种最大投注金额
    private String betMax = "";
    //当前选择币种最小投注金额
    private String betMin = "";
    //当前选择币种默认逃跑倍数
    private String escapeDefalut;
    //当前选择币种最小逃跑倍数
    private String escapeMin;
    //当前选择币种最大逃跑倍数
    private String escapeMax;
    //金币是否洒落
    private boolean isFall;
    //是否下注
    private boolean isBet;
    //是否逃跑
    private boolean isRun;
    //是否爆炸
    private boolean isBoom;
    //是否预约
    private boolean isSubscribe;
    //预约下注是否已请求
    private boolean isSubscriCommit;
    //预约下注金额
    private String subscribeBetMoney;
    //预约逃跑倍数
    private String subscribeBetValue;


    //下注币种
    private String betType;
    //下注金额
    private double betMoney;
    //下注成功的逃跑倍数
    private float betValue;
    //游戏公共信息
    private CommonBean.BuyInfoBean buyInfoBean;

    //逃跑集合
    private List<EscapeBean> escapeBeans;
    private List<String> gameCoins;
    private Map<String, Integer> icons = new HashMap<>();
    private FallObject fallObject;

    private boolean mActivityIsCreate = true;
    //用户ID
    private String userid = "";
    private Disposable subscribe;

    @Override
    public int getLayoutId() {
        return R.layout.boom_activity_boom;
    }

    @Override
    public BoomPresenter getPresenter() {
        return new BoomPresenter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getOpenAwardList();
//        if (boomView != null) {
//            boomView.setDrawing(true);
//        }
        if (mActivityIsCreate) {
            mPresenter.initWebSocket();
            mActivityIsCreate = false;
        }
        if (fallObject == null) {
            //初始化一个金币样式的fallObject
            FallObject.Builder builder = new FallObject.Builder(getResources().getDrawable(R.mipmap.boom_icon_gold));
            fallObject = builder
                    .setSpeed(40, true)
                    .setSize(80, 80, false)
                    .setWind(5, true, true)
                    .build();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
//        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
//            if (boomView != null) {
//                boomView.setDrawing(false);
//            }
//        }
        fallObject = null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.closeWebSocket();
        mPresenter.closeTimeSocket();
        if (subscribe != null) {
            DisposUtil.close(subscribe);
        }
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        etRunAway.setFilters(new InputFilter[]{new MoneyValueFilter()});
        etBet.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(5)});

        ImageUtil.setOriginImage(R.mipmap.boom_hundred, iv_boom_handred);
        if (fallObject == null) {
            //初始化一个金币样式的fallObject
            FallObject.Builder builder = new FallObject.Builder(getResources().getDrawable(R.mipmap.boom_icon_gold));
            fallObject = builder
                    .setSpeed(40, true)
                    .setSize(80, 80, false)
                    .setWind(5, true, true)
                    .build();
        }
        initSlideTabLayout();
        initBoomView();
        initRecyHistory();
        editTextListener();
    }

    private void initRecyHistory() {

        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        strings.add("3");
        strings.add("4");
        strings.add("5");
        recyclerViewHistory.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        recyclerViewHistory.addItemDecoration(new DividerHorizenLineItemDecoration(this)
                .setFirstDraw(false)
                .setLastDraw(false)
                .setTopPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 13))
                .setBottomPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 13))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 0.6f))
                .setPaintColor(ContextCompat.getColor(App.INSTANCE, R.color.color_f2))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.boom_item_five_history, new SlimInjector<OpenAwardBean>() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onInject(OpenAwardBean data, IViewInjector injector, List payloads) {
                if (slimAdapter.getData().indexOf(data) == 0 || slimAdapter.getData().indexOf(data) == 2) {
                    injector.textColor(R.id.tvScale, Color.parseColor("#80E055"));
                }
                if (slimAdapter.getData().indexOf(data) == 1) {
                    injector.textColor(R.id.tvScale, Color.parseColor("#E8F955"));
                }
                if (slimAdapter.getData().indexOf(data) == 3) {
                    injector.textColor(R.id.tvScale, Color.parseColor("#e6402b"));
                }
                if (slimAdapter.getData().indexOf(data) == 4) {
                    injector.textColor(R.id.tvScale, Color.parseColor("#E9B055"));
                }
                injector.text(R.id.tv_id, data.getPeriodID())
                        .text(R.id.tvScale, data.getBoomValue() + "X");
            }
        }).attachTo(recyclerViewHistory).updateData(new ArrayList());

    }

    @SuppressLint("CheckResult")
    private void editTextListener() {
        RxTextView.textChanges(etRunAway)
                .debounce(100, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .map(CharSequence::toString)
                .subscribe(s -> {
                    if (buyInfoBean == null) {
                        return;
                    }
                    if (TextUtils.isEmpty(s)) {
                        s = escapeDefalut;
                    }
                    if (!ArithUtil.compare(s, escapeMin)) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.boom_boomActivity1) + escapeMin);
                        s = escapeMin;
                    }
                    if (NumberUtil.isLittleNumber(s) && NumberUtil.isTwoLittleNumber(s)) {
                        etRunAway.setText(NumberUtil.keepDown(s, 1, false));
                        tvEarn.setText(App.INSTANCE.getString(R.string.boom_boomActivity2) + NumberUtil.keep2(ArithUtil.mul(Double.valueOf(getText(etRunAway)), isEmpty(etBet) ? 0 : Double.valueOf(getText(etBet)))));
                        return;
                    }
                    tvEarn.setText(App.INSTANCE.getString(R.string.boom_boomActivity2) + NumberUtil.keep2(ArithUtil.mul(Double.valueOf(s), isEmpty(etBet) ? 0 : Double.valueOf(getText(etBet)))));
                    tvChance.setText(App.INSTANCE.getString(R.string.boom_boomActivity3) + NumberUtil.keep2(100 / Float.parseFloat(s.trim())) + "%");
                });
        RxTextView.textChanges(etBet)
                .debounce(100, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .map(CharSequence::toString)
                .subscribe(s -> {
                    if (TextUtils.isEmpty(s)) {
                        s = "0";
                    }
                    if (buyInfoBean == null) {
                        return;
                    }
//                    if (s.startsWith("0") && s.length() > 1) {
//                        etBet.setText(s.substring(1, s.length()));
//                    }
                    if (Float.parseFloat(s) > Float.valueOf(betMax)) {
                        etBet.setText(betMax);
                        etBet.setSelection(etBet.getText().length());
                    }
//                    if (s.contains(".")) {
//                        etBet.setText(NumberUtil.keepDown(s, 0, false));
//                        tvEarn.setText(App.INSTANCE.getString(R.string.boom_boomActivity2) + NumberUtil.keep2(ArithUtil.mul(Double.valueOf(s), isEmpty(etRunAway) ? 0 : Double.valueOf(getText(etRunAway)))));
//                        return;
//                    }
                    tvEarn.setText(App.INSTANCE.getString(R.string.boom_boomActivity2) + NumberUtil.keep2(ArithUtil.mul(Double.valueOf(s), isEmpty(etRunAway) ? 0 : Double.valueOf(getText(etRunAway)))));
                });
    }

    @Override
    public void initData() {
        gameCoins = new ArrayList<>();
        gameCoins.add(Constans.COIN_AICASH);
        gameCoins.add(Constans.COIN_USDT);
        icons.put(Constans.COIN_AICASH, R.mipmap.icon_ai);
        icons.put(Constans.COIN_USDT, R.mipmap.icon_usdt);
        tvCoinName.setText(gameCoins.get(0));
        ivCoin.setImageResource(icons.get(getText(tvCoinName)));
        betType = gameCoins.get(0);
        mPresenter.getUserInfo();
        mPresenter.getCommonData();
        mPresenter.getOpenAwardList();
        /*获取用户信息*/
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                userid = userData.getUid();
                tvMoneyUsdt.setText(CommonUtil.dealCoinNum(userData.getToto_game_money(), Constans.COIN_USDT));
                tvMoneyCny.setText("≈" + NumberUtil.keep2(userData.getToto_game_money_cny()));
                tvCoinUsable.setText(App.INSTANCE.getString(R.string.boom_boomUsable) + CommonUtil.dealCoinNum(userData.getAicash_game_money(), Constans.COIN_AICASH) + Constans.COIN_AICASH);
            } else {
                userData = null;
                tvMoneyUsdt.setText("--");
                tvMoneyCny.setText("--");
            }
        });
        /*下注、逃跑、预约*/
        ClickUtil.click(300, btnBet, () -> {
            if (userData == null) {
                return;
            }
            if (buyInfoBean == null) {
                ToastUtils.show(App.INSTANCE.getString(R.string.boom_boomActivity4));
                mPresenter.getCommonData();
                return;
            }
            //准备阶段
            if (btnBet.getText().equals(App.INSTANCE.getString(R.string.boom_boomActivity5))) {
                if (checkBet()) {
                    mPresenter.normalBet(getText(tvCoinName).toLowerCase(), getText(etBet), getText(etRunAway));
                }
                return;
            }
            //游戏阶段
            if (isBet) {
                if (isRun) {
                    if (isSubscribe) {
                        isSubscribe = false;
                        subscribeBetMoney = "";
                        subscribeBetValue = "";
                    } else {
                        if (checkBet()) {
                            isSubscribe = true;
                            subscribeBetMoney = etBet.getText().toString().trim();
                            subscribeBetValue = etRunAway.getText().toString();
                        }
                    }
                } else {
                    mPresenter.normalRun();
                }
            } else {
                if (isSubscribe) {
                    isSubscribe = false;
                    subscribeBetMoney = "";
                    subscribeBetValue = "";
                } else {
                    if (checkBet()) {
                        isSubscribe = true;
                        subscribeBetMoney = etBet.getText().toString().trim();
                        subscribeBetValue = etRunAway.getText().toString();
//                        subscribeBetType = money_type;
                    }
                }
            }
        });
        /*爆点回调*/
        boomView.setBoomListener(new BoomView.BoomListener() {
            @Override
            public void boom() {
                runOnUiThread(() -> {
                    isBet = false;
                    isRun = false;
                    isBoom = true;
                    if (tv_now_value == null) {
                        return;
                    }
                    tv_now_value.setVisibility(View.INVISIBLE);
                    tv_show_run.setVisibility(View.INVISIBLE);
                    tv_now_value.setText("");
                    tv_show_run.setText("");
                    if (userData == null) {
                        if (btnBet != null) {
                            btnBet.setClickable(true);
                        }
                        btnBet.setText(App.INSTANCE.getString(R.string.boom_boomView1));
                        btnBet.setBackground(ContextCompat.getDrawable(App.INSTANCE, R.drawable.lib_solid969_cor10));
                        return;
                    }
                    if (btnBet != null) {
                        btnBet.setClickable(false);
                    }
                    if (isSubscribe) {
                        btnBet.setText(App.INSTANCE.getString(R.string.boom_boomActivity7));
                    } else {
                        btnBet.setText(App.INSTANCE.getString(R.string.boom_boomView1));

                    }
                    btnBet.setBackground(ContextCompat.getDrawable(BoomActivity.this, R.drawable.lib_solid969_cor10));
                });
            }

            @Override
            public void prePare() {
                runOnUiThread(() -> {
                    isRun = false;
                    isBoom = false;
                    if (tv_now_value == null) {
                        return;
                    }
                    tv_now_value.setVisibility(View.INVISIBLE);
                    tv_show_run.setVisibility(View.INVISIBLE);
                    tv_now_value.setText("");
                    tv_show_run.setText("");
                    if (userData == null) {
                        if (btnBet != null) {
                            btnBet.setClickable(true);
                        }
                        btnBet.setText(App.INSTANCE.getString(R.string.boom_boomActivity5));
                        btnBet.setBackground(ContextCompat.getDrawable(BoomActivity.this, R.drawable.lib_solid969_cor10));
                        return;
                    }
                    if (isBet) {
                        btnBet.setText(App.INSTANCE.getString(R.string.boom_boomActivity7));
                        if (btnBet != null) {
                            btnBet.setClickable(false);
                        }
                        btnBet.setBackground(ContextCompat.getDrawable(BoomActivity.this, R.drawable.lib_solid969_cor10));
                    } else {
                        btnBet.setText(App.INSTANCE.getString(R.string.boom_boomActivity5));
                        if (btnBet != null) {
                            btnBet.setClickable(true);
                        }
                        btnBet.setBackground(ContextCompat.getDrawable(App.INSTANCE, R.drawable.lib_gradient_common_selector));
                    }
                });
            }

            @Override
            public void onTextChange(String nowTime) {
                runOnUiThread(() -> {
                    if (tv_now_value == null) {
                        return;
                    }
                    if (btnBet != null) {
                        btnBet.setClickable(true);
                    }
                    if (Float.parseFloat(nowTime) > 100) {
                        if (iv_boom_handred.getVisibility() == View.GONE) {
                            iv_boom_handred.setVisibility(View.VISIBLE);
                        }
                    }
                    tv_now_value.setVisibility(View.VISIBLE);
                    tv_show_run.setVisibility(View.VISIBLE);
                    btnBet.setText("");
                    if (userData == null) {
                        tv_now_value.setText(App.INSTANCE.getString(R.string.boom_boomActivity8));
                        tv_show_run.setText(App.INSTANCE.getString(R.string.boom_boomActivity9));
                        btnBet.setBackground(ContextCompat.getDrawable(BoomActivity.this, R.drawable.lib_solid969_cor10));
                        return;
                    }
                    if (!isBet) {
                        if (!isSubscribe) {
                            tv_now_value.setText(App.INSTANCE.getString(R.string.boom_boomActivity8));
                            tv_show_run.setText(App.INSTANCE.getString(R.string.boom_boomActivity9));
                        } else {
                            tv_now_value.setText(App.INSTANCE.getString(R.string.boom_boomActivity10));
                            tv_show_run.setText(App.INSTANCE.getString(R.string.boom_boomActivity11));
                        }
                        btnBet.setBackground(ContextCompat.getDrawable(App.INSTANCE, R.drawable.lib_gradient_common_selector));
                        return;
                    }
                    if (isRun) {
                        if (!isSubscribe) {
                            tv_now_value.setText(App.INSTANCE.getString(R.string.boom_boomActivity8));
                            tv_show_run.setText(App.INSTANCE.getString(R.string.boom_boomActivity9));
                        } else {
                            tv_now_value.setText(App.INSTANCE.getString(R.string.boom_boomActivity10));
                            tv_show_run.setText(App.INSTANCE.getString(R.string.boom_boomActivity11));
                        }
                        btnBet.setBackground(ContextCompat.getDrawable(App.INSTANCE, R.drawable.lib_gradient_common_selector));
                        return;
                    }
                    tv_now_value.setText(CommonUtil.dealCoinNum(String.valueOf(ArithUtil.mul(Double.valueOf(betMoney), Double.valueOf(nowTime))), betType) + " " + betType);
                    tv_show_run.setText(App.INSTANCE.getString(R.string.boom_boomActivity12));
                    btnBet.setBackground(ContextCompat.getDrawable(App.INSTANCE, R.drawable.lib_gradient_common_selector));
                    if (Float.parseFloat(nowTime) > betValue) {
                        btnBet.setBackground(ContextCompat.getDrawable(App.INSTANCE, R.drawable.lib_gradient_common_selector));
                        tv_now_value.setText(App.INSTANCE.getString(R.string.boom_boomActivity8));
                        tv_show_run.setText(App.INSTANCE.getString(R.string.boom_boomActivity9));
                        isRun = true;
                        return;
                    }
                });
            }
        });
        /*选择币种*/
        ClickUtil.click(300, rl_choose_coin, () -> {
            BottomDialog.chooseCoins(BoomActivity.this, gameCoins, new BottomDialog.OnSure() {
                @Override
                public void onSure(String tv) {
                    betType = tv;
                    tvCoinName.setText(tv);
                    ivCoin.setImageResource(icons.get(getText(tvCoinName)));
                    tvCoinUsable.setText(App.INSTANCE.getString(R.string.boom_boomUsable)
                            + CommonUtil.dealCoinNum(
                            getText(tvCoinName).equals(Constans.COIN_USDT) ?
                                    userData.getUsdt_game_money() : userData.getAicash_game_money()
                            , getText(tvCoinName)) + getText(tvCoinName));
                    if (buyInfoBean == null) {
                        ToastUtils.show(App.INSTANCE.getString(R.string.boom_boomActivity4));
                        return;
                    }
                    updateCommonData();
                }
            });
        });
        /*可下注最大金额*/
        ClickUtil.click(300, tvMax, () -> {
            if (buyInfoBean == null) {
                ToastUtils.show(App.INSTANCE.getString(R.string.boom_boomActivity4));
                return;
            }
            etBet.setText(betMax);
        });
        /*可下注最小金额*/
        ClickUtil.click(300, tvMin, () -> {
            if (buyInfoBean == null) {
                ToastUtils.show(App.INSTANCE.getString(R.string.boom_boomActivity4));
                return;
            }
            etBet.setText(betMin);
        });
        /*二分之一*/
        ClickUtil.click(300, tvDivide, () -> {
            if (buyInfoBean == null) {
                ToastUtils.show(App.INSTANCE.getString(R.string.boom_boomActivity4));
                return;
            }
            if (isEmpty(etBet)) {
                return;
            }
            if (Float.parseFloat(etBet.getText().toString().trim()) / 2 < Float.parseFloat(betMin)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.boom_boomActivity13) + betMin);
                etBet.setText(betMin);
                return;
            }
            etBet.setText(Float.parseFloat(getText(etBet)) / 2 + "");
        });
        /*二倍*/
        ClickUtil.click(300, tvMultiply, () -> {
            if (buyInfoBean == null) {
                ToastUtils.show(App.INSTANCE.getString(R.string.boom_boomActivity4));
                return;
            }
            if (isEmpty(etBet)) {
                return;
            }
            if (Float.parseFloat(etBet.getText().toString().trim()) * 2 > Float.parseFloat(betMax)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.boom_boomActivity14) + betMax);
                etBet.setText(betMax);
                return;
            }
            etBet.setText(Float.parseFloat(getText(etBet)) * 2 + "");
        });
        /*帮助中心*/
        ClickUtil.click(300, tvRight, () -> {
            ARouter.getInstance().build(RConfig.BOOM_ACTIVITY_FAQ).navigation();
        });
    }

    private boolean checkBet() {
        if (TextUtils.isEmpty(etBet.getText().toString())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.boom_boomActivity15));
            return false;
        }
        if (TextUtils.isEmpty(etRunAway.getText().toString())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.boom_boomActivity16));
            return false;
        }
        if (Float.parseFloat(etRunAway.getText().toString().trim()) < 1.1) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.boom_boomActivity17));
            return false;
        }
        return true;
    }

    private void initBoomView() {
        boomView.setLineTextColor(ContextCompat.getColor(App.INSTANCE, R.color.color_646));
        boomView.setStrokeColor(ContextCompat.getColor(App.INSTANCE, R.color.color_646));
        boomView.setShowTextColor(ContextCompat.getColor(App.INSTANCE, R.color.color_da7));
        boomView.setBgColor(ContextCompat.getColor(App.INSTANCE, R.color.white));
    }

    private void initSlideTabLayout() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        ArrayList<String> titles = new ArrayList<>();
        titles.add(App.INSTANCE.getString(R.string.boom_boomActivity18));
        titles.add(App.INSTANCE.getString(R.string.boom_boomActivity19));
        titles.add(App.INSTANCE.getString(R.string.boom_boomActivity20));
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.BOOM_FRAGMENT_ALL_GUESS).navigation());
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.BOOM_FRAGMENT_MY_GUESS_HISTORY).navigation());
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.BOOM_FRAGMENT_OPEN_AWARD_HISTORY).navigation());
        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), titles, fragments));
        slidingTabLayout.setViewPager(viewPager);
    }

    /*游戏长链接*/
    public void getRunDataOK(StockBloomBean stockBloomBean) {
        if (stockBloomBean == null) {
            return;
        }
        if (stockBloomBean != null) {
//            tvAwardPoll.setText(App.INSTANCE.getString(R.string.boom_boomActivity21) + stockBloomBean.getData().get(0).getCrash().getStart_cny_pool_money() + " " + getText(tvCoinName));
            switch (stockBloomBean.getStatus()) {
                case 1:
                    isFall = false;
                    if (fallingView != null) {
                        fallingView.setVisibility(View.GONE);
                    }
                    if (iv_boom_handred.getVisibility() == View.VISIBLE) {
                        iv_boom_handred.setVisibility(View.GONE);
                    }
                    if (isSubscribe) {
                        if (TextUtils.isEmpty(subscribeBetMoney) || TextUtils.isEmpty(subscribeBetValue)) {
                            return;
                        }
                        if (!isSubscriCommit) {
                            mPresenter.normalBet(getText(tvCoinName).toLowerCase(), subscribeBetMoney, subscribeBetValue);
                            isSubscriCommit = true;
                        }
                    }
                    mPresenter.getUserInfo();
                    mPresenter.getOpenAwardList();
                    if (getText(tvCoinName).equals(Constans.COIN_USDT)) {
                        tvAwardPoll.setText(App.INSTANCE.getString(R.string.boom_boomActivity21) + stockBloomBean.getData().get(0).getPrepare().getStart_usdt_pool_money() + " USDT");
                    } else {
                        tvAwardPoll.setText(App.INSTANCE.getString(R.string.boom_boomActivity21) + stockBloomBean.getData().get(0).getPrepare().getStart_aicash_pool_money() + " AICASH");
                    }
                    boomView.gamePreparing((int) (Double.valueOf(stockBloomBean.getData().get(0).getPrepare().getSt())
                            - Double.valueOf(stockBloomBean.getData().get(0).getPrepare().getPt())));
                    break;
                case 2:
                    if (getText(tvCoinName).equals(Constans.COIN_USDT)) {
                        tvAwardPoll.setText(App.INSTANCE.getString(R.string.boom_boomActivity21) + stockBloomBean.getData().get(0).getProcess().getStart_usdt_pool_money() + " USDT");
                    } else {
                        tvAwardPoll.setText(App.INSTANCE.getString(R.string.boom_boomActivity21) + stockBloomBean.getData().get(0).getProcess().getStart_aicash_pool_money() + " AICASH");
                    }
                    if (Math.abs(stockBloomBean.getData().get(0).getProcess().getE() - boomView.getNowTime()) > 20) {
                        boomView.supendStart(stockBloomBean.getData().get(0).getProcess().getE());
                    } else {
                        boomView.gameStart();
                    }
                    boomView.supendStart(stockBloomBean.getData().get(0).getProcess().getE());
                    isSubscriCommit = false;
                    if (userData == null) {
                        return;
                    }
                    if (stockBloomBean.getData().get(2).getEscape_list().size() == 0) {
                        return;
                    }
                    escapeBeans = new ArrayList<>();
                    if (subscribe != null) {
                        DisposUtil.close(subscribe);
                    }
                    subscribe = Flowable.fromIterable(stockBloomBean.getData().get(2).getEscape_list())
                            .filter(xiazhuBeans -> xiazhuBeans.getUid().equals(userid))
                            .compose(RxSchedulersHelper.transformer())
                            .subscribe(xiazhuBeans -> {
                                        escapeBeans.add(new EscapeBean(
                                                xiazhuBeans.getRealname()
                                                , Float.parseFloat(xiazhuBeans.getFinal_beishu())));
                                    }
                                    , e -> {
                                        Log.i("escapeBeans", "getRunDataOK: " + e);
                                        e.printStackTrace();
                                    }
                                    , () -> {
                                        Log.i("escapeBeans", "");
                                        boomView.setEscapeBeans(escapeBeans);
                                    });
                    break;
                case 3:
                    if (!isFall) {
                        if (fallObject != null && fallingView != null) {
                            fallingView.setVisibility(View.VISIBLE);
                            fallingView.addFallObject(fallObject, 30);//添加50个下落物体对象
                            isFall = true;
                        }
                    }
                    if (getText(tvCoinName).equals(Constans.COIN_USDT)) {
                        tvAwardPoll.setText(App.INSTANCE.getString(R.string.boom_boomActivity21) + stockBloomBean.getData().get(0).getCrash().getStart_usdt_pool_money() + " USDT");
                    } else {
                        tvAwardPoll.setText(App.INSTANCE.getString(R.string.boom_boomActivity21) + stockBloomBean.getData().get(0).getCrash().getStart_aicash_pool_money() + " AICASH");
                    }
                    boomView.gameOver((float) stockBloomBean.getData().get(0).getCrash().getBoomValue());
                    break;
            }
        }
    }

    /*下注成功*/
    public void betSuccess(String type, String money, String escape_beishu) {
        this.isSubscribe = false;
        this.betMoney = Double.parseDouble(money);
        this.betValue = Float.parseFloat(escape_beishu);
        isRun = false;
        isBet = true;
    }

    /*下注失败*/
    public void betFail() {
        this.isSubscribe = false;
        isRun = false;
    }

    /*逃跑成功*/
    public void runSuccess() {
        isRun = true;
        isBet = false;
    }

    /*更新游戏基本信息（投注范围、逃跑范围、默认逃跑值）*/
    private void updateCommonData() {
        if (getText(tvCoinName).equals(Constans.COIN_USDT)) {
            betMin = buyInfoBean.getUsdt_info().getUsdt_buy_min_money();
            betMax = buyInfoBean.getUsdt_info().getUsdt_buy_max_money();
            escapeDefalut = buyInfoBean.getUsdt_info().getUsdt_escape_beishu();
            escapeMin = buyInfoBean.getUsdt_info().getUsdt_escape_beishu_min();
            escapeMax = buyInfoBean.getUsdt_info().getUsdt_escape_beishu_max();
        } else if (getText(tvCoinName).equals(Constans.COIN_AICASH)) {
            betMin = buyInfoBean.getAicash_info().getAicash_buy_min_money();
            betMax = buyInfoBean.getAicash_info().getAicash_buy_max_money();
            escapeDefalut = buyInfoBean.getAicash_info().getAicash_escape_beishu();
            escapeMin = buyInfoBean.getAicash_info().getAicash_escape_beishu_min();
            escapeMax = buyInfoBean.getAicash_info().getAicash_escape_beishu_max();
        }
        if (etBet != null && etRunAway != null) {
            etBet.setHint(App.INSTANCE.getString(R.string.boom_boomActivity22) + betMin + "-" + betMax);
            etBet.setText(betMin);
            etRunAway.setText(escapeDefalut);
        }
    }

    public void getCommonDataOK(CommonBean data) {
        if (data == null) {
            return;
        }
        buyInfoBean = data.getBuy_info();
        if (buyInfoBean == null) {
            return;
        }
        updateCommonData();
    }

    /*获取最新5条游戏记录*/
    public void getOpenAwardOK(List<OpenAwardBean> data) {
        if (slimAdapter != null) {
            slimAdapter.updateData(data);
        }
    }

    /*获取用户信息，更新拥有游戏币*/
    public void getUserInfoOK(UserData data) {
        if (data == null) {
            return;
        }
        this.userData = data;
        if (userViewModel != null) {
            userViewModel.update(data);
        }
        tvMoneyUsdt.setText(CommonUtil.dealCoinNum(data.getToto_game_money(), Constans.COIN_USDT));
        tvMoneyCny.setText("≈" + NumberUtil.keep2(data.getToto_game_money_cny()));

        tvCoinUsable.setText(App.INSTANCE.getString(R.string.boom_boomUsable)
                + CommonUtil.dealCoinNum(
                getText(tvCoinName).equals(Constans.COIN_USDT) ?
                        userData.getUsdt_game_money() : userData.getAicash_game_money()
                , getText(tvCoinName)) + getText(tvCoinName));
    }
}
